﻿using System;
using System.Collections.Generic;
 
using System.Text;
using GGSharp.Structures;

namespace GGSharp.Packets
{
    public class ReceivedMessagePacket : MessagePacketBase
    {
        protected const byte EXTENSION_RECIPIENTS = 0x1;
        protected const byte EXTENSION_RICHTEXT = 0x2;
        protected const byte EXTENSION_IMAGEREPLY_FIRST = 0x05;
        protected const byte EXTENSION_IMAGEREPLY_NEXT = 0x06;

        private int _time = 0;

        private List<MessageRichTextFormat> _richTextFormats = new List<MessageRichTextFormat>();
        private List<int> _recipients;
        private MessageImageReplyNext _imageData;

        public ReceivedMessagePacket()
        {
            this.Type = PacketType.GG_RECV_MSG;
        }

        public MessageImageReplyNext ImageData
        {
            get { return _imageData; }
        }

        public ICollection<int> Recipients
        {
            get { return _recipients; }
        }

        public ICollection<MessageRichTextFormat> RichTextFormats
        {
            get { return _richTextFormats; }
        }

        public override void Read(System.IO.BinaryReader reader)
        {
            base.Read(reader);

            //wczytanie podstawowych danych
            _recipient = reader.ReadInt32();
            _seq = reader.ReadInt32();
            _time = reader.ReadInt32();
            _class = reader.ReadInt32();

            this.ReadedLength = 16;

            //wczytanie tekstu wiadomosci
            _message = ReadMessageText(reader);

            this.ReadedLength += _message.Length + 1;

            //wczytanie dodatkowych parametrow wiadomosci
            while (this.ReadedLength < this.Length)
            {
                byte type = reader.ReadByte();
                this.ReadedLength += 1;

                switch (type)
                {
                    //informacje o formatowaniu tekstu
                    case EXTENSION_RICHTEXT:
                        _richTextFormats = new List<MessageRichTextFormat>();
                        short length = reader.ReadInt16();
                        short readedRichText = 0;

                        while (readedRichText < length)
                        {
                            MessageRichTextFormat msgRichTextFormat = new MessageRichTextFormat(reader);
                            readedRichText += (short)msgRichTextFormat.ReadedLength;
                            _richTextFormats.Add(msgRichTextFormat);
                        }

                        this.ReadedLength += length + 2;

                        break;
                    //zawiera kontakty do ktorych wyslano wiadomosc w trybie konferencji
                    case EXTENSION_RECIPIENTS:
                        _recipients = new List<int>();
                        int count = reader.ReadInt32();

                        for (int i = 0; i < count; i++)
                            _recipients.Add(reader.ReadInt32());

                        this.ReadedLength += 4 + count * 4;

                        break;
                    //przyszedl do nas fragment grafiki
                    case EXTENSION_IMAGEREPLY_FIRST:
                        _imageData = new MessageImageReplyFirst(reader);
                        this.ReadedLength += _imageData.ReadedLength;
                        break;
                    case EXTENSION_IMAGEREPLY_NEXT:
                        _imageData = new MessageImageReplyNext(reader);
                        this.ReadedLength += _imageData.ReadedLength;
                        break;
                    //jesli jest jakies nieznane rozszerzenie to nie mozna przewidziec dalszego formatu wiadomosci
                    default:
                        ReadToEnd((int)this.ReadedLength, reader);
                        System.Diagnostics.Debug.WriteLine("Blad odczytywania wiadomosci");
                        break;
                }
            }
        }

        public override void Write(System.IO.BinaryWriter writer)
        {
            throw new NotImplementedException();
            base.Write(writer);
        }
    }
}
